import java.io.IOException;
import java.io.OutputStream;


public abstract class BytesWriter {
	
	OutputStream out;
	public BytesWriter(OutputStream out) {
		this.out = out;
	}
	
	public static BytesWriter newBytesWriter(OutputStream out, boolean bigEndian) {
		if (bigEndian) {
			return new BEBytesWriter(out);
		} else {
			return new LEBytesWriter(out);
		}
	}
	
	abstract void write(byte b) throws IOException;
	abstract void write(short s) throws IOException;
	abstract void write(int i) throws IOException;
}

class BEBytesWriter extends BytesWriter {
	
	public BEBytesWriter(OutputStream out) {
		super(out);
	}

	void write(byte b) throws IOException {
		out.write(b & 0xFF);
	}
	
	void write(short s) throws IOException {
		byte b1 = (byte) (s & 0x00FF);
		byte b2 = (byte) ((s & 0xFF00) >>> 8);
		out.write(b1 & 0xFF);
		out.write(b2 & 0xFF);
	}
	
	void write(int i) throws IOException {
		byte b1 = (byte) (i & 0x00FF);
		byte b2 = (byte) ((i & 0xFF00) >>> 8);
		byte b3 = (byte) ((i & 0xFF00) >>> 16);
		byte b4 = (byte) ((i & 0xFF00) >>> 24);
		out.write(b1 & 0xFF);
		out.write(b2 & 0xFF);
		out.write(b3 & 0xFF);
		out.write(b4 & 0xFF);
	}
}

class LEBytesWriter extends BytesWriter {
	
	public LEBytesWriter(OutputStream out) {
		super(out);
	}

	void write(byte b) throws IOException {
		out.write(b & 0xFF);
	}
	
	void write(short s) throws IOException {
		byte b1 = (byte) (s & 0x00FF);
		byte b2 = (byte) ((s & 0xFF00) >>> 8);
		out.write(b1 & 0xFF);
		out.write(b2 & 0xFF);
	}
	
	void write(int i) throws IOException {
		byte b1 = (byte) (i & 0x00FF);
		byte b2 = (byte) ((i & 0xFF00) >>> 8);
		byte b3 = (byte) ((i & 0xFF00) >>> 16);
		byte b4 = (byte) ((i & 0xFF00) >>> 24);
		out.write(b1 & 0xFF);
		out.write(b2 & 0xFF);
		out.write(b3 & 0xFF);
		out.write(b4 & 0xFF);
	}
}